<link rel="stylesheet" href="/stylesheets/admin.css">
<link rel="stylesheet" href="/stylesheets/style.css">
<link href="/multiselect/css/multi-select.css" media="screen" rel="stylesheet" type="text/css">

<style>
    #page-control > li > a {
        font-size: 14px;
    }
</style>
<div class="row">
    <div class="col-xs-12">
        <div class="container-header">
            <p class="container-title"><%= platform %> 打包服务</p>
        </div>
        <div class="setting-div">
            <div class="setting-header-div">
                <div>
                    <div class="btn-group create-btn">
                        <button id="package-btn" type="button" class="btn btn-success">打包</button>
                        <button id="show-app" type="button" style="height: 34px" class="btn btn-default dropdown-toggle"
                                data-toggle="dropdown">暂无应用
                            <span class="caret"></span>
                        </button>
                        <ul id="app-list" class="dropdown-menu" role="menu">
                            <% apps.forEach(function (app) { %>
                            <li><a id="<%= app._id %>" onclick="changeApp(this)"><%= app.name %></a></li>
                            <% }) %>
                            <% if (apps.length > 0) { %>
                            <li class="divider"></li>
                            <% } %>
                            <% if (type === 1){ %>
                            <li><a href="/app?platform=ios&new=true">新建应用</a></li>
                            <% } else { %>
                            <li><a href="/app?platform=android&new=true">新建应用</a></li>
                            <% }%>
                        </ul>
                    </div>

                    <% if (type === 1 && user.role && user.role.permissions.packageA) {  %>
                    <a href="/package?platform=android" id="create-btn" class="list-header-btn btn btn-default">切换到 Android</a>
                    <% } else if (type === 2 && user.role && user.role.permissions.packageI) { %>
                    <a href="/package?platform=ios" id="create-btn" class="list-header-btn btn btn-default">切换到 iOS</a>
                    <% } %>
                    <% if (type === 1 && user.role && user.role.permissions.managerI){ %>
                    <a style="position: absolute;right: 120px;height: 34px;width: 80px;" href="/app?platform=ios" id="create-btn" class="list-header-btn btn btn-default">管理应用</a>
                    <% } else if (type === 2 && user.role && user.role.permissions.managerA) { %>
                    <a style="position: absolute;right: 120px;height: 34px;width: 80px;" href="/app?platform=android" id="create-btn" class="list-header-btn btn btn-default">管理应用</a>
                    <% }%>

                </div>
                <hr>
            </div>
            <div>
                <table class="table table-striped">
                    <thead>
                    <tr>
                        <th></th>
                        <th><input id="select-all" type="checkbox"></th>
                        <th>应用名称</th>
                        <th>版本</th>
                        <th>build</th>
                        <th>分支</th>
                        <th>结果</th>
                        <% if(type === 1) { %>
                        <th>代理机</th>
                        <% } %>
                        <th>通知机器人</th>
                        <th>操作者</th>
                        <th>操作时间</th>
                        <th>详情</th>
                    </tr>
                    </thead>
                    <tbody id="list-body">
                    </tbody>
                </table>
                <div style="position: relative">
                    <div style="position: absolute;left: 20px;height: 80px;">
                        <a id="del-select" style="margin-top: 20px" class="btn btn-default">删除已选择</a>
                    </div>

                    <div style="text-align: center;">
                        <ul class="pagination pagination-lg" id="page-control"></ul>
                    </div>
                </div>

            </div>
            <div class="setting-footer-div">

            </div>
        </div>
        <div style="height: 40px"></div>
    </div>
</div>
<link rel="stylesheet" href="/stylesheets/jquery-confirm.min-3.2.3.css">
<script src="/multiselect/js/jquery.multi-select.js" type="text/javascript"></script>
<!--<script src="/javascripts/date-extension.js" type="text/javascript"></script>-->
<script src="/javascripts/version-extension.js" type="text/javascript"></script>
<script src="/javascripts/jquery-confirm.min-3.1.1.js"></script>
<script language="JavaScript">
    var appId = null;
</script>

<% if (type === 1){ %>
<script src="/javascripts/package-ios.js" type="text/javascript"></script>
<% } else { %>
<script src="/javascripts/package-android.js" type="text/javascript"></script>
<% }%>
<script language="JavaScript">

    function changeApp(node) {
        var id = node.id;
        window.location.href = '/package?platform=<%= platform %>&app_id=' + id;
    }

    $(function () {
        let loadAppId = "<%= appId %>";
        if (loadAppId) {
            let id = loadAppId;
            $('#show-app').html($('#' + id)[0].innerText + '  ' +'<span class="caret"></span>');
            appId = id;
            loadPackageList(1);
        } else {
            let a = $('#app-list').find('a')[0];
            let id = a.id;
            if (id.length > 0) {
                a.click();
            }
        }
    });

    $('#del-select').click(function () {
        var selectList = [];
        $('td[id*="select-"]').each(function (index,item) {

            var cbox = item.childNodes[0];
            var select = ($(cbox).prop('checked'));
            if (select) {
                var id = cbox.parentNode.id.split('select-')[1];
                selectList.push(id);
            }
        });
        if (selectList.length === 0) {
            $.confirm({
                content: '您还没有选择要删除的包信息',
                title: '',
                animationBounce: 1.5,
                animation: 'rotateXR',
                closeAnimation: 'rotateYR',
                buttons: {
                    ok: {
                        text: "知道了",
                        btnClass: 'btn-success',
                        keys: ['enter'],
                        action: function(){

                        }
                    }
                }
            })
        } else {
            $.confirm({
                title: '确定要删除这'+selectList.length+'条包信息?',
                content: '一旦删除将无法恢复',
                animationBounce: 1.5,
                animation: 'rotateXR',
                closeAnimation: 'rotateYR',
                buttons: {
                    ok: {
                        text: "确定",
                        btnClass: 'btn-success',
                        keys: ['enter'],
                        action: function(){
                            $.post('/package/delete',{ids:JSON.stringify(selectList)},function (data) {
                                if (data.code === 0) {
                                    showSuccess('已删除');
                                    loadPackageList();
                                } else {
                                    showError(data.message);
                                }
                            })
                        }
                    },
                    cancel: {
                        text: "再想想",
                        btnClass: 'btn-default',
                        keys: ['cancel'],
                        action: function(){

                        }
                    }
                }
            })
        }
    })

    $('#select-all').on('click',function (e) {
        var target = e.target;
        var select = $(target).prop('checked');
        $('td[id*="select-"]').each(function (index,item) {
            var cbox = item.childNodes[0];
            if (!cbox.disabled) {
                $(cbox).prop('checked',select);
            }
        });
    });

    function loadPackageList(page) {
        $('#select-all').prop('checked',false);
        $.post("/package/list",{page:page || 1,id:appId},function (data) {
            if (data.code == 0) {
                var parent = document.getElementById('list-body');
                for (var i = parent.children.length - 1; i >= 0; i--) {
                    parent.removeChild(parent.children[i]);
                }
                var total = data.data.total;
                var page = data.data.page;
                var pagesize = data.data.pagesize;
                for (var i = 0 ; i < data.data.list.length ; i++) {
                    var package = data.data.list[i];
                    var packageNode = document.createElement('tr');
                    var id = package._id;
                    packageNode.id = id;

                    var emptyNode = document.createElement('td');


                    var selectNode = document.createElement('td');
                    selectNode.id = 'select-' + id;
                    if (package.status !== 0) {
                        selectNode.innerHTML = "<input type=\"checkbox\">";
                    } else {
                        selectNode.innerHTML = "<input type=\"checkbox\" disabled='disabled'>";
                    }
//                    selectNode.innerHTML = "<input type=\"checkbox\" disabled='disabled'>";

                    var nameNode = document.createElement('td');
                    nameNode.id = 'name-' + id;
//                    nameNode.innerHTML = "<a target='_blank' href='" + component.url + "' value='" + component.url + "'>" + component.name + "</a>";
                    nameNode.innerText = package.app ? package.app.name : '未知';
//                    var urlNode = document.createElement('td');
//                    urlNode.id = 'url-' + id;
//                    urlNode.innerHTML = "<a target='_blank' href='" + component.url + "' value='" + component.url + "'>点击查看</a>";

                    var versionNode = document.createElement('td');
                    versionNode.id = 'version-' + id;
                    versionNode.innerText = package.version;

                    var buildNode = document.createElement('td');
                    buildNode.innerText = package.build;

                    var branchNode = document.createElement('td');
                    branchNode.innerText = package.branch;

                    var resultNode = document.createElement('td');
                    var status = package.status === 1 ? '成功' : (package.status === -1 ? '失败' : '处理中');
                    resultNode.innerText = status;

                    <% if(type === 1) { %>
                    var delegaterNode = document.createElement('td');
                    delegaterNode.innerText = package.delegater ? package.delegater.name : '未知';
                    <% } %>

//                    var delegaterNode = document.createElement('td');
//                    delegaterNode.innerText = package.delegater.name;

                    var robotsNode = document.createElement('td');
//                    robotsNode.innerText = component.robots;
                    var robotHTML = '<ul style="list-style-type:none;padding-left: 0px">';
                    package.robots.forEach(function (robot) {
                        var add = '<li value="' + robot._id + '">' + robot.name + '</li>';
                        robotHTML += add;
                    })
                    robotHTML += '</ul>'
                    robotsNode.id = 'robots-' + id;
                    robotsNode.innerHTML = robotHTML;

                    var userNode = document.createElement('td');
                    userNode.innerText = package.user ? package.user.nickname : '未知用户';

                    var timeNode = document.createElement('td');
                    timeNode.innerText = package.createTime;

                    var  editNode = document.createElement('td');
                    editNode.innerHTML = "<a class='btn btn-default' target='_blank' href='/package/log/" + id +"'>详情</a>";

                    packageNode.appendChild(emptyNode);
                    packageNode.appendChild(selectNode);
                    packageNode.appendChild(nameNode);
                    packageNode.appendChild(versionNode);
                    packageNode.appendChild(buildNode);
                    packageNode.appendChild(branchNode);
                    packageNode.appendChild(resultNode);
                    <% if(type === 1) { %>
                    packageNode.appendChild(delegaterNode);
                    <% } %>
                    packageNode.appendChild(robotsNode);
                    packageNode.appendChild(userNode);
                    packageNode.appendChild(timeNode);
                    packageNode.appendChild(editNode);

                    parent.appendChild(packageNode);
                }
                var page_control = document.getElementById('page-control');
                for (var i = page_control.children.length - 1; i >= 0; i--) {
                    page_control.removeChild(page_control.children[i]);
                }

                if (total === 0) {
                    return;
                }

                var homePage = document.createElement('li');
                homePage.innerHTML = "<a onclick='loadPackageList(1)'>首页</a>"
                page_control.appendChild(homePage);

                if (parseInt(page) !== 1) {
                    var prePage = document.createElement('li');
                    prePage.innerHTML = "<a onclick='loadPackageList(" + (page - 1) + ")'>&lt;&nbsp;上一页</a>"
                    page_control.appendChild(prePage);
                }

                var pageCount = Math.ceil(total / pagesize);

                for (var i = 1 ; i <= pageCount; i++) {
                    var li = document.createElement('li');
                    if (i === parseInt(page)) {
                        li.className = 'active';
                    }
//                    li.className = 'btn';
                    li.innerHTML = "<a onclick='loadPackageList(" + i + ")'>" + i + "</a>"
                    page_control.appendChild(li);
                }
                if (parseInt(page) < pageCount) {
                    var nextPage = document.createElement('li');
                    nextPage.innerHTML = "<a onclick='loadPackageList(" + (parseInt(page) + 1) + ")'>下一页&nbsp;&gt;</a>"
                    page_control.appendChild(nextPage);
                }

                var lastPage = document.createElement('li');
                lastPage.innerHTML = "<a onclick='loadPackageList(" + pageCount + ")'>末页</a>"
                page_control.appendChild(lastPage);

            } else  {

            }
        })
    }
</script>